package com.cqie.hospitalescort.controller.file;

import com.cqie.hospitalescort.controller.BaseController;
import com.cqie.hospitalescort.controller.params.FileDTO;
import com.cqie.hospitalescort.exception.BusinessException;
import com.cqie.hospitalescort.model.Result;
import com.cqie.hospitalescort.service.file.AliYunService;
import com.cqie.hospitalescort.utils.Asserts;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@Api(value = "公共接口",tags = {"公共接口"})
@RequestMapping("/common")
public class CommonController extends BaseController {
    @Autowired
    private AliYunService aliYunService;

    @PostMapping("/upload")
    @ApiOperation(value = "上传文件",httpMethod = "POST",produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponse(code = 200,message = "成功",response = Result.class)
    public Result<List<String>> uploadFile(@RequestParam List<MultipartFile> files) {
        try {
            List<String> list = aliYunService.uploadFiles(files);
            return response(list);
        } catch (IOException e) {
            throw new BusinessException("上传失败！");
        }
    }

    @PostMapping("/delete")
    @ApiOperation(value = "上传文件",httpMethod = "POST",produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponse(code = 200,message = "成功",response = Result.class)
    public Result<?> deleteFile(@RequestBody FileDTO dto) {
        Asserts.notEmpty(dto.getUrls(),"文件路径不能为空！");
        try {
            aliYunService.deleteFilesByUrl(dto.getUrls());
        }catch (Exception e){
            throw new BusinessException("删除失败！");
        }
        return response();
    }
}
